package com.smartfast4j.common.test;

import com.smartfast4j.common.file.FileService;
import com.smartfast4j.common.file.FileUtil;
import com.smartfast4j.common.file.QiNiuFileServiceImpl;
import com.smartfast4j.common.file.bean.UploadResult;
import org.apache.log4j.Logger;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * Created by gacl on 2017/10/16.
 */
public class FileUploadTest3 {

    private static Logger logger = Logger.getLogger(FileUploadTest3.class);

    private static FileService fileService;

    /**
     * 针对所有测试，只执行一次，且必须为static void
     */
    @BeforeClass
    public static void init() {
        fileService = QiNiuFileServiceImpl.getInstance();
    }

    @Test
    public void testUploadFile1(){
        try {
            FileInputStream fileInputStream = new FileInputStream("D:\\测试数据\\Cg2BwVmv-oCAPA91AAJ6fgJZatg503.jpg");
            UploadResult uploadResult = fileService.uploadFile(fileInputStream, "Cg2BwVmv-oCAPA91AAJ6fgJZatg503.jpg");
            logger.debug("uploadResult="+uploadResult);
        } catch (FileNotFoundException e) {
            logger.error(e.getMessage(),e);
        }
    }

    @Test
    public void testUploadFile2(){
        try {
            FileInputStream fileInputStream = new FileInputStream("D:\\测试数据\\4.png");
            UploadResult uploadResult = fileService.uploadFile(fileInputStream, "4.png","/gaclXDP");
            logger.debug("uploadResult="+uploadResult);
        } catch (FileNotFoundException e) {
            logger.error(e.getMessage(),e);
        }
    }

    @Test
    public void testUploadFile3(){
        try {
            File file = new File("D:/测试数据/me.jpg");
            UploadResult uploadResult = fileService.uploadFile(file, "/gacl/test");
            logger.debug("uploadResult="+uploadResult);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }

    @Test
    public void testUploadFile4(){
        try {
            FileInputStream fileInputStream = new FileInputStream("D:\\测试数据\\4.png");
            UploadResult uploadResult = fileService.uploadFile(FileUtil.input2ByteArray(fileInputStream), "4.png");
            logger.debug("uploadResult="+uploadResult);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }

    @Test
    public void testUploadFile5(){
        try {
            FileInputStream fileInputStream = new FileInputStream("D:\\测试数据\\Cg2BwVmv-oCAPA91AAJ6fgJZatg503.jpg");
            UploadResult uploadResult = fileService.uploadFile(FileUtil.input2ByteArray(fileInputStream), "Cg2BwVmv-oCAPA91AAJ6fgJZatg503.jpg","/gacl/test");
            logger.debug("uploadResult="+uploadResult);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }

    @Test
    public void testUploadFile6(){
        try {
            UploadResult uploadResult = fileService.uploadFile("D:\\测试数据\\4.png");
            logger.debug("uploadResult="+uploadResult);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }

    @Test
    public void testUploadFile7(){
        try {
            UploadResult uploadResult = fileService.uploadFile("D:/测试数据/Cg2BwVmv-oCAPA91AAJ6fgJZatg503.jpg","/gacl/test");
            logger.debug("uploadResult="+uploadResult);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }

    @Test
    public void testUploadNetworkFile1(){
        try {
            UploadResult uploadResult = fileService.uploadNetworkFile("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1508751925&di=d23ac8ad28fb2046936f47f991b7e2ca&imgtype=jpg&er=1&src=http%3A%2F%2Fwww.gwchina.cn%2Fuploadfile%2F2016%2F0804%2F20160804015824783.jpg");
            logger.debug("uploadResult="+uploadResult);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }

    @Test
    public void testUploadNetworkFile2(){
        try {
            UploadResult uploadResult = fileService.uploadNetworkFile("http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLJ9jRgnPbNfJGZThlDNNvNd8ulqGhQO34tL82PZEVWGzG4OHIWtM2xazcAnjpZzZ3xLfeEic4cvIg/0","png");
            logger.debug("uploadResult="+uploadResult);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }
}
